﻿using System;
using System.Management;
using ClusterAwareProperties.Properties;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Wrapper class NetworkInterface ManagementObject
    /// </summary>
    public class NetworkInterfaceProperty : IDisposable
    {
        #region private property

        private readonly ManagementObject _classInstance;

        #endregion

        #region ctor

        /// <summary>
        /// constuctor
        /// </summary>
        private NetworkInterfaceProperty()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="queryobj">NetworkInterface ManagementObject instance</param>
        public NetworkInterfaceProperty(ManagementObject queryobj)
            : this()
        {
            var path = queryobj.ToString();
            if (path.IndexOf("\\root\\MSCluster:MSCluster_NetworkInterface") == -1)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_TypeClassInvalid, path, "\\root\\MSCluster:MSCluster_NetworkInterface"));
            }
            _classInstance = queryobj;
            this.Convert(queryobj);
        }

        #endregion

        #region public properties

        /// <summary>
        /// Return instance of NetworkInterface ManagementObject
        /// </summary>
        public ManagementObject Instance
        {
            get { return _classInstance; }
        }


        /// <summary>
        /// Provides a short textual description of the network interface.
        /// </summary>
        public string Caption { get; internal set; }

        /// <summary>
        /// Indicates the current status of the network interface.
        /// </summary>
        public StatusResource Status { get; internal set; }

        /// <summary>
        /// Indicates when the network interface was installed. 
        /// A lack of a value does not indicate that the network interface is not installed.
        /// </summary>
        public DateTime? InstallDate { get; internal set; }

        /// <summary>
        /// Indicates the scoping system's CreationClassName.
        /// </summary>
        public string SystemCreationClassName { get; internal set; }

        /// <summary>
        /// Indicates the scoping system's Name.
        /// </summary>
        public string SystemName { get; internal set; }

        /// <summary>
        /// Indicates the name of the class or the subclass used in the creation of an instance.
        /// </summary>
        public string CreationClassName { get; internal set; }

        /// <summary>
        /// Provides an address or other identifying information that uniquely identifies the network interface.
        /// </summary>
        public string DeviceId { get; internal set; }

        /// <summary>
        /// If TRUE, the node, with its running operating system, supports power management.
        /// If FALSE, the integer value 1 ("Not Supported") should be the only entry in the PowerManagementCapabilities array. 
        /// This Boolean does not indicate that power management features are currently enabled, or if enabled, what features are supported. 
        /// For more information, see the PowerManagementCapabilities array.
        /// </summary>
        public bool PowerManagementSupported { get; internal set; }

        /// <summary>
        /// Indicates the specific power-related capabilities of the network interface.
        /// </summary>
        public PowerCapacity[] PowerManagementCapabilities { get; internal set; }

        /// <summary>
        /// The availability and status of the network interface.
        /// </summary>
        public AvailabilityNetwork Availability { get; internal set; }

        /// <summary>
        /// Indicates the Win32 Configuration Manager error code.
        /// </summary>
        public ConfigManagerErrorCode ConfigManagerErrorCode { get; internal set; }

        /// <summary>
        /// If TRUE, the device is using a user-defined configuration.
        /// </summary>
        public bool ConfigManagerUserConfig { get; internal set; }

        /// <summary>
        /// Indicates the Win32 Plug and Play device identifier of the logical device.
        /// Example: "*PNP030b"
        /// </summary>
        public string PnpDeviceId { get; internal set; }

        /// <summary>
        /// Indicates whether the network interface is in an enabled, disabled, other, or unknown state.
        /// </summary>
        public StatusInfoNetwork StatusInfo { get; internal set; }

        /// <summary>
        /// Captures the last error code reported by the network interface.
        /// </summary>
        public UInt32 LastErrorCode { get; internal set; }

        /// <summary>
        /// Supplies more information about the error recorded in the LastErrorCode property and information on any corrective actions that may be taken.
        /// </summary>
        public string ErrorDescription { get; internal set; }

        /// <summary>
        /// Indicates that the error reported in the LastErrorCode property is now cleared.
        /// </summary>
        public bool ErrorCleared { get; internal set; }

        /// <summary>
        /// Captures additional data, in addition to the DeviceID property, that can be used to identify a network interface.
        /// </summary>
        public string[] OtherIdentifyingInfo { get; internal set; }

        /// <summary>
        /// Indicates the number of hours since the last power cycle of the network interface.
        /// </summary>
        public UInt64 PowerOnHours { get; internal set; }

        /// <summary>
        /// Indicates the total number of hours that the network interface has been powered.
        /// </summary>
        public UInt64 TotalPowerOnHours { get; internal set; }

        /// <summary>
        /// Represents an array of free-form strings that provide explanations and details behind the entries in the OtherIdentifyingInfo property array. 
        /// Noted that each entry of this array is related to the entry in the OtherIdentifyingInfo property that is located at the same index.
        /// </summary>
        public string[] IdentifyingDescriptions { get; internal set; }

        /// <summary>
        /// Provides the Clustering Service–generated name for the network interface.
        /// </summary>
        public string Name { get; internal set; }

        /// <summary>
        /// Provides comments about the network interface.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Provides the name that is used to uniquely identify the network interface in the cluster.
        /// </summary>
        public string Adapter { get; internal set; }

        /// <summary>
        /// Provides a unique ID for the network interface.
        /// </summary>
        /// <remarks>
        /// Windows Server 2003:  
        /// This property is not supported.
        /// </remarks>
        public string AdapterId { get; internal set; }

        /// <summary>
        /// Provides the name of the node in which the network interface is installed.
        /// </summary>
        /// <remarks>
        /// Windows Server 2003:  
        /// This property is not supported.
        /// </remarks>
        public string Node { get; internal set; }

        /// <summary>
        /// Provides the primary network address that the node uses for the network interface.
        /// </summary>
        public string Address { get; internal set; }

        /// <summary>
        /// Provides the name of the network to which the network interface is connected.
        /// </summary>
        public string Network { get; internal set; }

        /// <summary>
        /// Indicates the current state of the network interface. The following are the possible values.
        /// </summary>
        public StateNetworkInterface State { get; internal set; }

        /// <summary>
        /// Provides access to the flags set for the network interface. For a list of possible flags, see CLUSCTL_NETINTERFACE_GET_FLAGS.
        /// </summary>
        public UInt32 Flags { get; set; }

        /// <summary>
        /// Provides access to the characteristics set for the network interface. For a list of possible characteristics, see CLUSCTL_NETINTERFACE_GET_CHARACTERISTICS.
        /// </summary>
        public CharacteristisNetwork Characteristics { get; set; }

        /// <summary>
        /// Provides the IPv6 addresses currently assigned to this network interface.
        /// </summary>
        /// <remarks>
        /// Windows Server 2003:  
        /// This property is not supported.
        /// </remarks>
        public string[] Ipv6Addresses { get; internal set; }

        /// <summary>
        /// Provides the IPv4 addresses currently assigned to this network interface.
        /// </summary>
        /// <remarks>
        /// Windows Server 2003:  
        /// This property is not supported.
        /// </remarks>
        public string[] Ipv4Addresses { get; internal set; }

        /// <summary>
        /// Indicates if DHCP is enabled on the network interface.
        /// </summary>
        /// <remarks>
        /// Windows Server 2003:  
        ///This property is not supported.
        /// </remarks>
        public bool? DhcpEnabled { get; internal set; }

        /// <summary>
        /// Represents the private properties of the network interface.
        /// </summary>
        public PrivatePropertiesData PrivateProperties { get; set; }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Dispose Class
        /// </summary>
        public void Dispose()
        {
            if (Instance != null)
            {
                Instance.Dispose();
            }
        }

        #endregion
    }
}
